package cn.codergege.gradle.plugin.kotlin

import cn.codergege.gradle.plugin.Stage
import cn.codergege.gradle.plugin.convertProjectName
import cn.codergege.gradle.plugin.generateSourceCode
import org.gradle.api.tasks.TaskAction
import java.io.File

open class KtBasicStage: Stage() {
    var ktName = "kotlin"

    var mainKtDir = File(mainDir, ktName)
    var testKtDir = File(testDir, ktName)

    var convertedProjectName = convertProjectName(project)
    var mainFileName = "${convertedProjectName}Application.kt"
    var mainTestFileName = "${convertedProjectName}ApplicationTest.kt"

    @TaskAction
    override fun run() {
        println("开始生成 kotlin 脚手架...")
        super.run()
        // 生成 main 文件
        generateSourceCode(mainKtDir, packageDir, mainFileName, codeAppMain)
        // 生成 main test 文件
        generateSourceCode(testKtDir, packageDir, mainTestFileName, codeAppTest)
    }

    open var codeAppMain = """package $packageName

fun main(args: Array<String>) {
    println("${convertedProjectName}Application 开始运行...")
}
"""
    open var codeAppTest = """package $packageName

import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class ${convertedProjectName}ApplicationTest {
    @Test
    fun testApp() {
        println("测试...")
        assertTrue(true)
    }
}
"""
}